package com.bit.frige.basket;

import java.util.ArrayList;

import com.bit.frige.MainActivity;
import com.bit.frige.R;
import com.bit.frige.DB.BasketDTO;
import com.bit.frige.DB.DBContactHelper;

import android.os.Bundle;
import android.app.Activity;
import android.util.SparseBooleanArray;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class BasketMainActivity extends Activity {
	ListView listView;
	EditText edt;
	Button btnadd;
	Button btndel;
	ArrayList<BasketDTO> BasketList = new ArrayList<BasketDTO>();
	ArrayAdapter<String> adapter;
	ArrayList<String> list = new ArrayList<String>();
	Button btn1;
	DBContactHelper db = new DBContactHelper(this);

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_basket_main);
		// listView화면에 출력
		String str;
		BasketList = db.getBasket();
		for (int i = 0; i < BasketList.size(); i++) {
			str = BasketList.get(i).getName();
			list.add(str);
		}

		// Generate list View from ArrayList
		btnadd = (Button) findViewById(R.id.button1);
		btndel = (Button) findViewById(R.id.button2);
		edt = (EditText) findViewById(R.id.EditText);
		listView = (ListView) findViewById(R.id.basketListView);

		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_multiple_choice, list);

		listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);// 하나의 항목을 설정할 수
																// 있는 모드로 설정
		listView.setAdapter(adapter);

		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				String item = list.get(position);
				Toast.makeText(getApplicationContext(),
				// basket.getName()+"를 선택하셨습니다.",
						item + "를 선택하셨습니다.", Toast.LENGTH_LONG).show();
			}
		});

		btnadd.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String str = edt.getText().toString();
				if(!str.equals("")){
					db.addBasket(str);
					list.add(str);
					adapter.notifyDataSetChanged();
					edt.setText("");
				}else{
					
					Toast.makeText(getApplicationContext(),
									 "물품을 입력하세요", Toast.LENGTH_SHORT).show();
				}
				
			}
		});

		btn1 = (Button) findViewById(R.id.homeicon);
		// 홈이벤트
		btn1.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Intent intentHome = new Intent(getApplicationContext(),
						MainActivity.class);
				intentHome.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				intentHome.putExtra("load", 1);
				startActivity(intentHome);
			}
		});

		btndel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				
				int a;
				int index;
				SparseBooleanArray check = listView.getCheckedItemPositions();

				for (a = 0; a < check.size(); a++) {
					check = listView.getCheckedItemPositions(); // 갱신된 리스트뷰를 토대로
																// 재설정
					for (index = 0; index < list.size(); index++) {
						if (check.get(index))
							break;
					}
					list.remove(index);
					db.deleteBasket(list.get(index));
					adapter.notifyDataSetChanged(); // 삭제 이후 ListView가 변경됬음을 다시
													// 갱신
				}

				listView.clearChoices();
				adapter.notifyDataSetChanged();
				 



			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.basket_main, menu);
		return true;
	}

}